package com.aerozhonghuan.logisticsmall.modules.home.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.common.entity.SearchProductHistory;
import com.aerozhonghuan.logisticsmall.widget.AutoLineFeedLayoutManager;
import com.blankj.utilcode.util.KeyboardUtils;

import java.util.Collections;

/**
 * 描述
 *
 * @author zhangyonghui
 * 创建日期 2019-05-30
 */
public class HistoryKeyFragment extends TitlebarFragment {
    private View rootView;
    private SearchAcrivity mActivity;
    private RelativeLayout relClear;
    private TextView tvNohistory;
    private RecyclerView recyclerHistory;
    private HistoryItemAdapter historyItemAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_search_historylist, null);
            initView();
            initData();
        }
        return rootView;
    }

    private void initView() {
        mActivity = (SearchAcrivity)getActivity();
        relClear = (RelativeLayout) rootView.findViewById(R.id.rel_clear);
        tvNohistory = (TextView) rootView.findViewById(R.id.tv_nohistory);
        recyclerHistory = (RecyclerView) rootView.findViewById(R.id.grid_search_history);
        recyclerHistory.setLayoutManager(new AutoLineFeedLayoutManager());
        rootView.findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (SearchProductHistory product : mActivity.mSearchProductHistories) {
                    long id = product.getTimeId();
                    mActivity.mDbAdapter.open().deleteSearchProductData(String.valueOf(id));
                }
                mActivity.mSearchProductHistories.clear();
                setEvaluateListAdapter();
                relClear.setVisibility(View.GONE);
                tvNohistory.setVisibility(View.VISIBLE);
            }
        });
    }

    private void initData() {
        mActivity.initData();
        if (mActivity.mSearchProductHistories.size() == 0) {
            relClear.setVisibility(View.GONE);
            tvNohistory.setVisibility(View.VISIBLE);
        } else {
            relClear.setVisibility(View.VISIBLE);
            tvNohistory.setVisibility(View.GONE);
        }
        Collections.reverse(mActivity.mSearchProductHistories);
        setEvaluateListAdapter();
    }


    // 设置保养项目adapter
    private void setEvaluateListAdapter() {
        if (historyItemAdapter == null) {
            historyItemAdapter = new HistoryItemAdapter();
            recyclerHistory.setAdapter(historyItemAdapter);
        } else {
            historyItemAdapter.notifyDataSetChanged();
        }
    }

    private class HistoryItemAdapter extends RecyclerView.Adapter<HistoryItemAdapter.MyViewHodler> {

        @Override
        public MyViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(), R.layout.item_search_history, null);
            return new MyViewHodler(view);
        }

        @Override
        public void onBindViewHolder(MyViewHodler holder, final int position) {
            final String procutName = mActivity.mSearchProductHistories.get(position).getProcutName();
            holder.btnHistory.setText(procutName);
            holder.btnHistory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    KeyboardUtils.hideSoftInput(getActivity());
                    mActivity.getTitleBarView().setEditText(procutName);
                    mActivity.searchProduct(procutName);
                }
            });
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return mActivity.mSearchProductHistories.size();
        }

        public class MyViewHodler extends RecyclerView.ViewHolder{
            Button btnHistory;
            public MyViewHodler(View itemView) {
                super(itemView);
                btnHistory = (Button) itemView.findViewById(R.id.btn_history);
            }
        }
    }
}
